using Rosetta.Runtime;
using Sirenix.OdinInspector;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Rosetta.Editor.Creator
{
    [Serializable]
    public struct PotInfo
    {
        [LabelText("@Global.GetTr(\"BugsAddress\")")]
        public string BugsAddress;

        [LabelText("@Global.GetTr(\"CopyrightHolder\")")]
        public string CopyrightHolder;

        [LabelText("@Global.GetTr(\"CopyrightYear\")")]
        public string CopyrightYear;

        [LabelText("@Global.GetTr(\"PackageName\")")]
        public string PackageName;

        [LabelText("@Global.GetTr(\"PackageVersion\")")]
        public string PackageVersion;
    }

    [CreateAssetMenu(menuName = "Rosetta/Creator/PotCreator")]
    [Serializable]
    public class PotCreator : CreatorBase
    {
        [LabelText("@Global.GetTr(\"Info\")")]
        public PotInfo Info;

        [LabelText("@Global.GetTr(\"PotTitle\")")]
        public string PotTitle;

        [LabelText("@Global.GetTr(\"RuntimeSetting\")")]
        public RosettaRuntimeSetting RuntimeSetting;

        protected override void _create()
        {
            DirectoryCreate($"{OutputPath}/res/font/{Name}");
            if (RuntimeSetting == null)
                RuntimeSetting = Resources.Load<RosettaRuntimeSetting>("RosettaRuntimeSetting");
            var str = "";
            str += GetHead();
            Collectors.ForEach(collector =>
            {
                collector.Collect(Name);
                if (collector.I18NStrings != null)
                {
                    foreach (var i18NString in collector.I18NStrings.Values)
                    {
                        str += GetComment(i18NString.Comment, i18NString.PathList);
                        str += GetBody(i18NString.Value) + "\n";
                    }
                }
            });
            SaveFile($"{OutputPath}/{Name}.pot", str);
        }


        private string GetHead()
        {
            var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            if (RuntimeSetting == null)
            {
                Debug.LogError("RuntimeSetting is null, Please Create RosettaRuntimeSetting");
                return "";
            }
            string hear = string.Empty;
            if (!string.IsNullOrEmpty(PotTitle))
                hear += $"# {PotTitle}\n";

            if (!string.IsNullOrEmpty(Info.CopyrightYear))
                hear += $"# Copyright (C) {Info.CopyrightYear} {Info.CopyrightHolder}\n";

            if (!string.IsNullOrEmpty(Info.PackageName))
                hear += $"# This file is distributed under the same license as the {Info.PackageName} package.\n";

            hear += "msgid \"\"\n";
            hear += "msgstr \"\"\n";

            if (!string.IsNullOrEmpty(Info.PackageName))
                hear += $"\"Project-Id-Version: {Info.PackageName} {Info.PackageVersion}\\n\"\n";

            if (!string.IsNullOrEmpty(Info.BugsAddress))
                hear += $"\"Report-Msgid-Bugs-To: {Info.BugsAddress}\"\n";

            hear += $"\"POT-Creation-Date: {time}\\n\"\n";
            hear += $"\"PO-Revision-Date: {time}\\n\"\n";
            hear += $"\"Language: {RuntimeSetting.DevLocale.ToString().ToLower()}\\n\"\n\n";
            return hear;
        }

        private string GetComment(string comment, List<string> paths)
        {
            var value = "";
            if (comment != "") value += $"#. {comment}";
            foreach (var path in paths) value += $"#: {path}\n";
            return value;
        }

        private string GetBody(string value)
        {
            return $"msgid \"{value.Replace(Convert.ToChar(10).ToString(), "\\n")}\"\nmsgstr \"\"\n";
        }
    }
}